<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="support.*, java.util.*, java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	student stu = (student)session.getAttribute("student");
	DegreeContainer degrees = (DegreeContainer)session.getAttribute("DegreeContainer");
	//String specialization = request.getParameter( "specialization" );
	Connection conn = null;
	PreparedStatement pStmt = null;
	ResultSet rs = null;

    // Registering Postgresql JDBC driver with the DriverManager
    Class.forName("org.postgresql.Driver");

    // Open a connection to the database using DriverManager
    conn = DriverManager.getConnection(
        "jdbc:postgresql://localhost/postgres?" +
        "user=postgres&password=cse135");
    
    conn.setAutoCommit(false);
    Statement stmt = conn.createStatement();
    Integer app_id = null;
    Integer add_id = null;
    Integer deg_id = null;
    rs = stmt.executeQuery("SELECT MAX(app_id) FROM applicants");
    if(rs.next()){	
 	    app_id = new Integer(1 + rs.getInt(1));
 		//System.out.println(rs.getString("firstname"));
    }
    
  	String insertStr = "INSERT INTO applicants VALUES (?, ?, ?, ?)";
    // Create the statement
    pStmt = conn.prepareStatement(insertStr);

	pStmt.setString(1, stu.getAttribute("firstName"));
	pStmt.setString(2, stu.getAttribute("lastName"));
	pStmt.setString(3, stu.getAttribute("middleName"));
	pStmt.setInt(4, app_id.intValue());
	pStmt.executeUpdate();
	
	rs = stmt.executeQuery("SELECT MAX(add_id) FROM addresses");
    if(rs.next()){	
 	    add_id = new Integer(1 + rs.getInt(1));
 		//System.out.println(rs.getString("firstname"));
    }
	insertStr = "insert into addresses values (?,?,?,?,?,?,?,?,?,?,?)";
	pStmt = conn.prepareStatement(insertStr);
	pStmt.setString(1, stu.getAttribute("citizenship"));
	pStmt.setString(2, stu.getAttribute("residence"));
	pStmt.setString(3, stu.getAttribute("street"));
	pStmt.setString(4, stu.getAttribute("zip"));
	pStmt.setString(5, stu.getAttribute("phone"));
	pStmt.setString(6, stu.getAttribute("city"));
	pStmt.setString(7, stu.getAttribute("state"));
	pStmt.setString(8, stu.getAttribute("phoneCode"));
	pStmt.setString(9, stu.getAttribute("resident"));
	pStmt.setInt(10, add_id.intValue());
	pStmt.setInt(11, app_id.intValue());
	pStmt.executeUpdate();
	
	
	for(int i = 0; i < degrees.getSize(); i++){
		rs = stmt.executeQuery("SELECT MAX(deg_id) FROM degrees");
	    if(rs.next()){	
	 	    deg_id = new Integer(1 + rs.getInt(1));
	 		//System.out.println(rs.getString("firstname"));
	    }
		insertStr = "insert into degrees values (?,?,?,?,?,?,?,?,?,?)";
		pStmt = conn.prepareStatement(insertStr);
		pStmt.setString(1, degrees.getDegree(i).getAttribute("location"));
		pStmt.setString(2, degrees.getDegree(i).getAttribute("university"));
		pStmt.setString(3, degrees.getDegree(i).getAttribute("major"));
		pStmt.setString(4, degrees.getDegree(i).getAttribute("month"));
		pStmt.setString(5, degrees.getDegree(i).getAttribute("year"));
		pStmt.setString(6, degrees.getDegree(i).getAttribute("gpa"));
		pStmt.setString(7, degrees.getDegree(i).getAttribute("title"));
		pStmt.setString(8, degrees.getDegree(i).getAttribute("specialization"));
		pStmt.setInt(9, deg_id.intValue());
		pStmt.setInt(10, app_id.intValue());
		pStmt.executeUpdate();
	}
	//Generate ID
	//app_id = ((int)Math.random()) % 100;
	

	//// Commit transaction
	conn.commit();
	conn.setAutoCommit(true);
	pStmt.close();
	conn.close();

%>
<%
out.println(stu.printAttributes());
%>
</body>
</html>